package com.china08.yxyapi.api.manage.edu.office.outernotice;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter.YxyNotifictionSchool;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyNoticeOutterRepository;

/**
 * 教育局行政办公 外部通知 将通知更改为已读状态
 * 
 */
@CrossOrigin
@RestController("OuterNoticeUpdIsReadController")
public class OuterNoticeUpdIsReadController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeOutterRepository yxyNoticeOutterRepository;

    @RequestMapping(value = "/api_yw/manage/edu/office/outerNotice/receive/updRead", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String schoolId = getProfile(request, response).getSchoolId();
        int isRead = 0;

        List<YxyNoticeOutter> yxyNoticeOutterLst = yxyNoticeOutterRepository
                .findByIdAndReceiveSchoolSchoolIdAndReceiveSchoolIsRead(reqModel.id, schoolId, isRead);
        for (YxyNoticeOutter yxyNoticeOutter : yxyNoticeOutterLst) {
            List<YxyNotifictionSchool> yxyNotifictionSchoollist = yxyNoticeOutter.getReceiveSchool();
            for (YxyNotifictionSchool yxyNotifictionSchool : yxyNotifictionSchoollist) {
                if (yxyNotifictionSchool.getIsRead() == 0 && schoolId.equals(yxyNotifictionSchool.getSchoolId())) {
                    yxyNotifictionSchool.setIsRead(1);
                }
            }
        }
        yxyNoticeOutterRepository.save(yxyNoticeOutterLst);
        RespModel respModel = new RespModel();
        respModel.setMessage("已读");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        // 消息id
        public String id;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}